package com.welike.shibo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 配置类
 *
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-09 01:08
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

    /**
     * 配置过滤规则
     *
     * @return FilterRegistrationBean
     */
//    @Bean
//    public FilterRegistrationBean jwtFilterRegistration() {
//        FilterRegistrationBean registration = new FilterRegistrationBean();
//        registration.setFilter(new JwtFilter());
//        registration.setName("JwtFilter");
//        registration.addUrlPatterns("/*");
//        registration.setOrder(1);
//        return registration;
//    }

    /**
     * 添加CORS映射
     *
     * @param registry 注册
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE", "PATCH")
                .allowedHeaders("Origin", "X-Requested-With", "Content-Type", "Accept", "x-auth-token")
                .allowCredentials(false).maxAge(3600);
    }

    /**
     * 添加静态资源目录
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/");

        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/fonts/element-icons.535877f.woff")
                .addResourceLocations("classpath:/static/fonts/element-icons.535877f.woff");
        registry.addResourceHandler("/fonts/element-icons.732389d.ttf")
                .addResourceLocations("classpath:/static/fonts/element-icons.732389d.ttf");
        registry.addResourceHandler("/img/**")
                .addResourceLocations("classpath:/static/img/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/js");
        registry.addResourceHandler("/assets/**")
                .addResourceLocations("classpath:/assets/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        super.addResourceHandlers(registry);
    }

//    @Override
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")
//                .excludePathPatterns("/**.html", "/v2/**", "/swagger-resources/**", "/webjars/**", "/swagger-resources/**", "/swagger-ui.html/**");
//    }
}
